package cn.ojh.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.ojh.domain.Article;
import cn.ojh.domain.ArticleContent;
import cn.ojh.mapper.ArticleContentMapper;
import cn.ojh.mapper.ArticleMapper;
import cn.ojh.service.IArticleService;

@Service
public class ArticleServiceImpl implements IArticleService{

	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private ArticleContentMapper articleContentMapper;
	
	@Override
	public List<Article> getAll() {
		// TODO Auto-generated method stub
		return articleMapper.selectAll();
	}

	@Override
	public List<Article> getArticleByCode(String articleTypeCode) {
		// TODO Auto-generated method stub
		return articleMapper.getArticleByCode(articleTypeCode);
	}

	@Override
	public Article getArticle(Long id) {
		// TODO Auto-generated method stub
		Article article = articleMapper.selectByPrimaryKey(id);
		ArticleContent articleContent = articleContentMapper.getContent(article.getId());
		article.setArticleContent(articleContent);
		return article;
	}

	@Override
	public void save(Article article) {
		// TODO Auto-generated method stub
		article.setTime(new Date());
		if(article.getId() == null) {			
			article.setUserName("admin");
			article.setCount(0);
			articleMapper.insert(article);
			ArticleContent articleContent = article.getArticleContent();
			articleContent.setArticleId(article.getId());
			articleContentMapper.insert(articleContent);
		}else {
			articleMapper.updateByPrimaryKey(article);
			articleContentMapper.updateByPrimaryKey(article.getArticleContent());
		}
	}

	@Override
	public ArticleContent getContent(Long articleId) {
		// TODO Auto-generated method stub
		
		return articleContentMapper.getContent(articleId);
	}

	@Override
	public void delete(Long articleId) {
		// TODO Auto-generated method stub
		articleMapper.deleteByPrimaryKey(articleId);
	}

}






